package com.smcdd.cdb.dbo.cdb_dbo.ui.widget;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.adapter.PagePopupAdapter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.ActionItem;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.Holder;

import java.util.ArrayList;

/**
 * 作者：Administrator on 2017/7/13 14:14
 * 邮箱：zhanghuaiha@gmail.com
 *    已安装设备的popupWindow
 */

public class PagePopup extends PopupWindow {
    private Context mContext;

    // 列表弹窗的间隔
    protected final int LIST_PADDING = 10;

    // 实例化一个矩形
    private Rect mRect = new Rect();

    // 坐标的位置（x、y）
    private final int[] mLocation = new int[2];

    // 屏幕的宽度和高度
    private int mScreenWidth, mScreenHeight;

    // 判断是否需要添加或更新列表子类项
    private boolean mIsDirty ;

    // 位置不在中心
    private int popupGravity = Gravity.NO_GRAVITY;

    // 弹窗子类项选中时的监听
    private OnItemOnClickListener mItemOnClickListener;

    // 定义列表对象
    private RecyclerView recycler;
    private TextView pop_page_title;
    private TextView on_line,off_line,wifi,mima,paper;
    private TextView three,five,seven;
    private TextView tv1,tv2,tv3,tv4,tv5,tv6,tv7;
    private String type0,type1,type2,type3,type4;
    private TextView pop_page_cancel,pop_page_save;

    // 定义弹窗子类项列表
    private ArrayList<ActionItem> mActionItems = new ArrayList<ActionItem>();

    public PagePopup(Context context) {
        // 设置布局的参数
        this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public PagePopup(Context context, int width, int height) {
        this.mContext = context;

        // 设置可以获得焦点
        setFocusable(true);
        // 设置弹窗内可点击
        setTouchable(true);
        // 设置弹窗外可点击
        setOutsideTouchable(true);

        // 获得屏幕的宽度和高度
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        mScreenWidth = wm.getDefaultDisplay().getWidth();
        mScreenHeight = wm.getDefaultDisplay().getHeight();

        // 设置弹窗的宽度和高度
        setWidth(width);
        setHeight(height);

        setBackgroundDrawable(new BitmapDrawable());

        // 设置弹窗的布局界面
        setContentView(LayoutInflater.from(mContext).inflate(
                R.layout.popup_title,null));
        setAnimationStyle(R.style.AnimHead);
        initUI();
    }

    /**
     * 初始化弹窗列表
     */
    private void initUI() {
        pop_page_title = (TextView) getContentView().findViewById(R.id.pop_page_title);
        pop_page_cancel = (TextView) getContentView().findViewById(R.id.pop_page_cancel);
        pop_page_save = (TextView) getContentView().findViewById(R.id.pop_page_save);
        on_line = (TextView) getContentView().findViewById(R.id.pop_page_online);
        off_line = (TextView) getContentView().findViewById(R.id.pop_page_offline);
        three = (TextView) getContentView().findViewById(R.id.pop_page_three);
        five = (TextView) getContentView().findViewById(R.id.pop_page_five);
        seven = (TextView) getContentView().findViewById(R.id.pop_page_seven);
        wifi = (TextView) getContentView().findViewById(R.id.pop_page_wifi);
        mima = (TextView) getContentView().findViewById(R.id.pop_page_mima);
        paper = (TextView) getContentView().findViewById(R.id.pop_page_paper);
        tv1 = (TextView) getContentView().findViewById(R.id.pop_page_tv1);
        tv2 = (TextView) getContentView().findViewById(R.id.pop_page_tv2);
        tv3 = (TextView) getContentView().findViewById(R.id.pop_page_tv3);
        tv4 = (TextView) getContentView().findViewById(R.id.pop_page_tv4);
        tv5 = (TextView) getContentView().findViewById(R.id.pop_page_tv5);
        tv6 = (TextView) getContentView().findViewById(R.id.pop_page_tv6);
        tv7 = (TextView) getContentView().findViewById(R.id.pop_page_tv7);
        type2 = "-1";
        type1 = "-1";
        type0 = "-1";
        type3 = "-1";
        type4 = "-1";

//        if (mItemOnClickListener != null)
//            mItemOnClickListener.onItemClick(scan);
        pop_page_cancel.setOnClickListener(v -> dismiss());
        pop_page_save.setOnClickListener(v -> {

            if (mItemOnClickListener != null){
                if (!type0.equals("1")) {
                    pop_page_title.setSelected(false);
                }
                mItemOnClickListener.onItemClick(type0, type1, type2, type3,type4);
            }
            dismiss();
        });
        //默认排序
        pop_page_title.setOnClickListener(v -> {
            type0 = "1";
            type1 = "-1";
            type2 = "-1";
            type3 = "-1";
            type4 = "-1";
            pop_page_title.setSelected(true);
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(false);
            three.setSelected(false);
            on_line.setSelected(false);
            five.setSelected(false);
            seven.setSelected(false);
            off_line.setSelected(false);
            wifi.setSelected(false);
            mima.setSelected(false);
            paper.setSelected(false);
        });

        //只看在线
        on_line.setOnClickListener(v -> {
            type1 = "1";
            type3 = "-1";
            type0 = "-1";
            on_line.setSelected(true);
            three.setSelected(false);
            off_line.setSelected(false);
            five.setSelected(false);
            seven.setSelected(false);
        });
        //只看离线
        off_line.setOnClickListener(v -> {
            type0 = "-1";
            type1 = "0";
            type3 = "-1";
            type4 = "1";
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            on_line.setSelected(false);
            off_line.setSelected(true);
        });
        //wifi设备
        wifi.setOnClickListener(v -> {
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });
        //密码
        mima.setOnClickListener(v -> {
            mima.setSelected(true);
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(false);
            three.setSelected(false);
            on_line.setSelected(false);
            five.setSelected(false);
            seven.setSelected(false);
            off_line.setSelected(false);
            wifi.setSelected(false);
            paper.setSelected(false);
            type0 = "-1";
            type1 = "-1";
            type2 = "-1";
            type3 = "-1";
            type4 = "4";
        });
        paper.setOnClickListener(v -> {
            mima.setSelected(false);
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(false);
            three.setSelected(false);
            on_line.setSelected(false);
            five.setSelected(false);
            seven.setSelected(false);
            off_line.setSelected(false);
            wifi.setSelected(false);
            paper.setSelected(true);
            type0 = "-1";
            type1 = "-1";
            type2 = "-1";
            type3 = "-1";
            type4 = "5";
        });

        //离线3天以上
        three.setOnClickListener(v -> {
            type0 = "-1";
            type1 = "0";
            type3 = "3";
            type4 = "1";
            three.setSelected(true);
            on_line.setSelected(false);
            five.setSelected(false);
            seven.setSelected(false);
            off_line.setSelected(true);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);


        });
        //离线5天以上
        five.setOnClickListener(v -> {
            type3 = "5";
            type1 = "0";
            type0 = "-1";
            three.setSelected(false);
            on_line.setSelected(false);
            five.setSelected(true);
            seven.setSelected(false);
            off_line.setSelected(true);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });

        //离线7天以上
        seven.setOnClickListener(v -> {
            type0 = "-1";
            type1 = "0";
            type3 = "7";
            type4 = "1";
            three.setSelected(false);
            on_line.setSelected(false);
            five.setSelected(false);
            seven.setSelected(true);
            off_line.setSelected(true);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);

        });

        //电量从高到低
        tv1.setOnClickListener(v -> {
            type0 = "-1";
            type2 = "desc";
            type4 = "1";
            tv1.setSelected(true);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(false);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
        });

        //电量从低到高
        tv2.setOnClickListener(v -> {
            type2 = "asc";
            type0 = "-1";
            tv1.setSelected(false);
            tv2.setSelected(true);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(false);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });

        //电量80%以上
        tv3.setOnClickListener(v -> {
            type2 = "5";
            type0 = "-1";
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(true);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(false);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });
        //60-80%
        tv4.setOnClickListener(v -> {
            type2 = "4";
            type0 = "-1";
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(true);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(false);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });
        //40-60%
        tv5.setOnClickListener(v -> {
            type2 = "3";
            type0 = "-1";
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(true);
            tv6.setSelected(false);
            tv7.setSelected(false);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });
        //20-40%
        tv6.setOnClickListener(v -> {
            type2 = "2";
            type0 = "-1";
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(true);
            tv7.setSelected(false);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });
        //20%以下
        tv7.setOnClickListener(v -> {
            type2 = "1";
            type0 = "-1";
            tv1.setSelected(false);
            tv2.setSelected(false);
            tv3.setSelected(false);
            tv4.setSelected(false);
            tv5.setSelected(false);
            tv6.setSelected(false);
            tv7.setSelected(true);
            mima.setSelected(false);
            wifi.setSelected(true);
            paper.setSelected(false);
            type4 = "1";
        });



    }


    /**
     * 显示弹窗列表界面
     */
    public void show(View view) {
        // 获得点击屏幕的位置坐标
        view.getLocationOnScreen(mLocation);

        // 设置矩形的大小
        mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(),
                mLocation[1] + view.getHeight());

        // 判断是否需要添加或更新列表子类项
        populateActions();
        if (mIsDirty) {
        }

        // 显示弹窗的位置
        showAtLocation(view, popupGravity, mScreenWidth - LIST_PADDING
                - (getWidth() / 2), mRect.bottom);
    }

    /**
     * 设置弹窗列表子项
     */
    private void populateActions() {
        mIsDirty = false;

    }

    /**
     * 添加子类项
     */
    public void addAction(ActionItem action) {
        if (action != null) {
            mActionItems.add(action);
            mIsDirty = true;
        }
    }

    /**
     * 清除子类项
     */
    public void cleanAction() {
        if (mActionItems.isEmpty()) {
            mActionItems.clear();
            mIsDirty = true;
        }
    }

    /**
     * 根据位置得到子类项
     */
    public ActionItem getAction(int position) {
        if (position < 0 || position > mActionItems.size())
            return null;
        return mActionItems.get(position);
    }

    /**
     * 设置监听事件
     */
    public void setItemOnClickListener(
            OnItemOnClickListener onItemOnClickListener) {
        this.mItemOnClickListener = onItemOnClickListener;
    }

    /**
     * @author yangyu 功能描述：弹窗子类项按钮监听事件
     */
    public static interface OnItemOnClickListener {
        public void onItemClick(String type0,String type1,String type2,String type3,String type4);
    }
}
